/*
 * File:         Destination.java
 * Author:       Robert Bittle <guywithnose@gmail.com>
 */
package twss;

import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

import org.json.JSONException;
import org.json.JSONString;
import org.json.JSONObject;

/**
 * The Class Destination.
 */
public class Thing implements DataObject, JSONString {

	/** The ID. */
	private long ID = 0;

	/** The name. */
	private String name;

	/**
	 * Get ID.
	 *
	 * @return the ID
	 */
	public long getID() {
		return this.ID;
	}

	/**
	 * Instantiates a new destination.
	 *
	 * @param id the id
	 */
	public Thing(long id) {
		ID = id;
		load();
	}

	/**
	 * Instantiates a new destination.
	 */
	public Thing() {
	}

	/**
	 * Get Name.
	 *
	 * @return the name
	 */
	public String getName() {
		return name;
	}

	/**
	 * Sets the name.
	 *
	 * @param Name the new name
	 */
	public void setName(String Name) {
		this.name = Name;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see triperest.DataObject#delete()
	 */
	@Override
	public boolean delete() {
		try {
			if (DataConnection.connect()) {
				String statement = "delete from thing where id = ?";
				PreparedStatement stmt = DataConnection.getConnection()
						.prepareStatement(statement);
				stmt.setLong(1, ID);
				int success = 2;
				success = stmt.executeUpdate();
				return success == 1;
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return true;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see triperest.DataObject#save()
	 */
	@Override
	public boolean save() {
		try {

			if (ID != 0) {
				return update();
			}
			return insert();
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return false;
	}

	/**
	 * Update.
	 * 
	 * @return true, if successful
	 * @throws SQLException
	 *             the SQL exception
	 */
	private boolean update() throws SQLException {
		if (DataConnection.connect()) {
			String statement = "update thing set name = ? where id = ?";
			PreparedStatement stmt = DataConnection.getConnection()
					.prepareStatement(statement);
			stmt.setString(1, name);
			stmt.setLong(2, ID);
			int success = 2;
			stmt.executeUpdate();
			return success == 1;
		}
		return false;
	}

	/**
	 * Update.
	 * 
	 * @return true, if successful
	 * @throws SQLException
	 *             the sQL exception
	 */
	private boolean insert() throws SQLException {
		if (DataConnection.connect()) {
			String statement = "INSERT INTO thing (name) VALUES ( ? )";
			PreparedStatement stmt = DataConnection.getConnection()
					.prepareStatement(statement,
							Statement.RETURN_GENERATED_KEYS);
			stmt.setString(1, name);
			int success = 2;
			stmt.executeUpdate();
			ResultSet key = stmt.getGeneratedKeys();
			if (key.next()) {
				ID = key.getLong(1);
			}
			return success == 1;
		}
		return false;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see triperest.DataObject#load()
	 */
	@Override
	public boolean load() {
		try {
			if (DataConnection.connect()) {
				String statement = "select name from thing where id = ?";
				PreparedStatement stmt = DataConnection.getConnection()
						.prepareStatement(statement);
				stmt.setLong(1, ID);
				ResultSet results = stmt.executeQuery();
				if (results.next()) {
					name = results.getString("name");
					return true;
				}
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return false;
	}

	/* (non-Javadoc)
	 * @see org.json.JSONString#toJSONString()
	 */
	@Override
	public String toJSONString() {
		JSONObject JO = new JSONObject();
		try {
			JO.put("id", ID);
			JO.put("name", name);
		} catch (JSONException e) {
			//Do Nothing
		}
		return JO.toString();
	}

}
